package com.crm.business.admin.service.impl;

import cn.hutool.core.util.StrUtil;
import com.crm.business.admin.service.AdminFileService;
import com.crm.business.admin.service.IAdminFileService;
import com.crm.common.admin.entity.BO.AdminDeleteByBatchIdBO;
import com.crm.common.ka.core.common.Result;
import com.crm.common.ka.core.servlet.upload.FileEntity;
import com.crm.common.ka.core.servlet.upload.UploadEntity;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.Collection;
import java.util.List;


@Service
public class AdminFileCallServiceImpl implements AdminFileService {

    @Autowired
    private IAdminFileService adminFileService;

    /**
     * 根据batchId查询文件列表
     *
     * @param batchId batchId
     * @return data
     */
    public List<FileEntity> queryFileList(String batchId){
        List<FileEntity> entityList = adminFileService.queryFileList(batchId);
        return entityList;
    }

    /**
     * 根据fileId查询文件
     *
     * @param fileId 文件ID
     * @return data
     */
    public FileEntity queryById(Long fileId){
        FileEntity fileEntity = adminFileService.queryById(fileId);
        return fileEntity;
    }

    public List<FileEntity> queryByIds(Collection<Long> fileIds){
        List<FileEntity> fileEntitys = adminFileService.queryByIds(fileIds);
        return fileEntitys;
    }

    /**
     * 根据batchId查询单个文件
     *
     * @param batchId batchId
     * @return data
     */
    public FileEntity queryOne(String batchId){
        FileEntity fileEntity = adminFileService.queryOneByBatchId(batchId);
        return fileEntity;
    }

    /**
     * 根据fileId删除文件
     *
     * @param fileId 文件ID
     * @return data
     */
    public void delete(Object fileId){
        if (fileId instanceof Number) {
            adminFileService.deleteById(((Number) fileId).longValue());
        } else if (fileId instanceof String) {
            if(((String) fileId).length() == 19){
                adminFileService.deleteById(Long.valueOf(fileId.toString()));
            }else {
                AdminDeleteByBatchIdBO bo = new AdminDeleteByBatchIdBO();
                bo.setBatchId((String) fileId);
                adminFileService.deleteByBatchId(bo);
            }
        }

        return ;
    }

    /**
     * 根据batchId删除文件
     *
     * @param batchId batchId
     * @return data
     */
    public void delete(List<String> batchId){
        adminFileService.deleteByBatchId(batchId);
        return ;
    }

    public Integer queryNum(List<String> batchId){
        Integer num = adminFileService.queryNum(batchId);
        return num;
    }

    public List<FileEntity> queryFileList(List<String> batchIdList){
        List<FileEntity> fileEntities = adminFileService.queryFileList(batchIdList);
        return fileEntities;
    }

    public String copyJxcImg(String batchId){
        String newBatchId = adminFileService.copyJxcImg(batchId);
        return newBatchId;
    }


    public void saveBatchFileEntity(List<String> adminFileIdList, String batchId){
        adminFileService.saveBatchFileEntity(adminFileIdList, batchId);
    }

    public UploadEntity upload(MultipartFile file,
                               String batchId,
                               String type, String isPublic) throws IOException {
        if (StrUtil.isEmpty(isPublic)){
            isPublic = "0";
        }
        UploadEntity entity = adminFileService.upload(file, batchId, type,isPublic);
        return entity;
    }
}
